$(function() {
	admin.mallOrder.init();
});
var saveFlag = false;
Namespace('admin.mallOrder', {
	init : function() {
		easyui.util.initDatagrid('#datagrid', {
			onDblClickRow : function() {
				var row = $('#datagrid').datagrid('getSelected');
				admin.mallOrder.detail(row.id);
			}
		});
	},
	query : function() {
		if (!$('#query-form').form('validate'))
			return;
		var data = $('#query-form').form('jsonObject');
		$('#datagrid').datagrid('load', data);
		admin.mallOrder.getSumTotal();
	},
	list : function(shopId,shopName) {
		top.addTab('#orderlist'+shopId,shopName+'的订单', contextPath+'/mall/order/list/'+shopId);
	},
	detail:function(id) {
		if (id != null) {
			var row = $('#datagrid').datagrid('getSelected');
			top.addTab(id,'订单详情', contextPath+'/mall/order/detail?orderNo=' + row.orderNo);
		}
	},
	toSend:function(id) {
		if(id==null) {
			top.showInfo('请选择需要修改的记录!');
			return;
		}
		getDialog('data-dialog').dialog({
			href:contextPath+'/mall/order/toSend/'+id,
			title: '订单发货',
		    width: 500,
		    height: 450,
		    buttons:[{
				text:'确定',
				iconCls:'icon-ok',
				handler:function() {
					admin.mallOrder.doSend();
				}
			},{
				text:'取消',
				iconCls:'icon-cancel',
				handler:function(){
					$('#data-dialog').dialog('close');
				}
			}]
		});
	},
	doSend:function() {
		if(!$('#data-form').form('validate')) return;
		var data=$('#data-form').form('jsonObject');
		$('#data-dialog').dialog('close');
		
		$('#datagrid').datagrid('loading');
		var row = $('#datagrid').datagrid('getSelected');
		$.postJson(contextPath+'/mall/order/sended',data,function(result) {
			$('#datagrid').datagrid('loaded');
			if(result.code==1) {
				top.showInfo('提交成功!');
				$('#datagrid').datagrid('reload');
			} else {
				if (result.msg) {
					top.showInfo(result.msg);
				} else {
					top.showInfo('提交失败!');
				}
			}
		});
	},
	cancel:function(id) {
		if (id != null) {
			$.messager.confirm('提示信息', '确定取消订单吗?', function(sure) {
				if (!sure) return;
				$.postJson(contextPath+'/order/base/adminCancel',{id:id},function(result) {
					if(result.code==1) {
						top.showInfo('订单取消成功!');
						$('#datagrid').datagrid('reload');
					} else {
						top.showInfo('订单取消失败：'+result.msg);
					}
				});
			});
		}
	},
	qqStoreSend:function(orderNo, shopId) {
		$.messager.confirm('提示信息', '确定发货吗?', function(sure) {
			var data = {
					'orderNo' : orderNo,
					'hasNeedDelivery' : 2,
					'shopId' : shopId,
					'sendDeliveryType':1
			};
			$.postJson(contextPath+'/mall/order/sended',data,function(result) {
				$('#datagrid').datagrid('loaded');
				if(result.code==1) {
					top.showInfo('发货成功!');
					$('#datagrid').datagrid('reload');
				} else {
					if (result.msg) {
						top.showInfo(result.msg);
					} else {
						top.showInfo('发货失败!');
					}
				}
			});
		});
	},
	getSumTotal : function() {//汇总订单金额【根据查询条件】
		var url = contextPath + '/mall/order/sumTotal';
		var shopId = $('#shopId').val();
		var orderNo = $('#orderNo').val();
		var orderType = $('#orderType').val();
		var orderStatus = $('#orderStatus').val();
		var payStatus = $('#payStatus').val();
		var deliveryStatus = $('#deliveryStatus').val();
		
		var endAtStr = $("input[name=endAtStr]").val();//$('#endAtStr').val();
		var startAtStr = $("input[name=startAtStr]").val();//$('#startAtStr').val();
		var params ={};
		var paramstr ="";
		if(shopId!='' && shopId!=null){
			paramstr+="&shopId="+shopId;
		}
		if(orderNo!='' && orderNo!=null){
			paramstr+="&orderNo="+orderNo;
		}
		if(orderType!='' && orderType!=null){
			paramstr+="&orderType="+orderType;
		}
		if(orderStatus!='' && orderStatus!=null){
			paramstr+="&orderStatus="+orderStatus;
		}
		if(payStatus!='' && payStatus!=null){
			paramstr+="&payStatus="+payStatus;
		}
		if(deliveryStatus!='' && deliveryStatus!=null){
			paramstr+="&deliveryStatus="+deliveryStatus;
		}
		
		if(startAtStr!='' && startAtStr!=null){
			paramstr+="&startAtStr="+startAtStr;
		}
		if(endAtStr!='' && endAtStr!=null){
			paramstr+="&endAtStr="+endAtStr;
		}
		if(paramstr!=''){
			url += "?"+paramstr;
		}
		$.post(url,params,function(result) {
			if(result.code==1) {
				$('#sumTotal').html(result.map.sumTotal);
			} else {
				$('#sumTotal').html("---");
			}
		});
		
	}
});